﻿using ProjScan.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    //
    // 摘要:
    //     GUI绘制工具类 - GUITools - Class36
    internal class GUITools
    {
        public class DirectoryListUI
        {
            public enum DrawOption
            {
                label,
                textField
            }

            public List<string> directoryList;

            private string label;

            private string addButtonLabel;

            private float textFieldWidth;

            private DrawOption drawOption;

            public DirectoryListUI(ref List<string> directoryList, string label, string addButtonLabel, DrawOption drawOption)
            {
                textFieldWidth = 0f;
                this.directoryList = directoryList;
                this.label = label;
                this.addButtonLabel = addButtonLabel;
                this.drawOption = drawOption;
            }

            public DirectoryListUI(ref List<string> directoryList, string label, string addButtonLabel, float textFieldWidth, DrawOption drawOption)
            {
                this.textFieldWidth = 0f;
                this.directoryList = directoryList;
                this.label = label;
                this.addButtonLabel = addButtonLabel;
                this.textFieldWidth = textFieldWidth;
                this.drawOption = drawOption;
            }

            public void SetDirectoryList(ref List<string> directoryList)
            {
                if (this.directoryList != directoryList)
                {
                    this.directoryList = directoryList;
                }
            }

            public void SetTextFieldWidth(float width)
            {
                textFieldWidth = width;
            }

            public void Draw()
            {
                if (textFieldWidth != 0f)
                {
                    ApplyLayoutOption(textFieldWidth, bool_0: true);
                }
                else
                {
                    DrawStyledVerticalLayout(drawShadow: true);
                }

                GUILayout.Label(label + $"(已添加 {directoryList.Count} 个): ");
                if (directoryList.Count == 0)
                {
                    GUILayout.Label("List is Empty");
                }

                for (int i = 0; i < directoryList.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    switch (drawOption)
                    {
                        default:
                            GUILayout.Label(directoryList[i]);
                            break;
                        case DrawOption.textField:
                            if (textFieldWidth > 0f)
                            {
                                GUILayoutOption[] options = new GUILayoutOption[2]
                                {
                                GUILayout.Width(textFieldWidth - 200f),
                                GUILayout.Height(20f)
                                };
                                directoryList[i] = GUILayout.TextField(directoryList[i], options);
                            }
                            else
                            {
                                GUILayoutOption[] options2 = new GUILayoutOption[2]
                                {
                                GUILayout.Width(800f),
                                GUILayout.Height(20f)
                                };
                                directoryList[i] = GUILayout.TextField(directoryList[i], options2);
                            }

                            break;
                        case DrawOption.label:
                            GUILayout.Label(directoryList[i]);
                            break;
                    }

                    GUILayout.FlexibleSpace();
                    GUIStyle gUIStyle = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button));
                    gUIStyle.fontSize = 14;
                    gUIStyle.fixedWidth = 75f;
                    gUIStyle.alignment = TextAnchor.MiddleLeft;
                    if (GUILayout.Button("删除[ - ]", gUIStyle))
                    {
                        directoryList.RemoveAt(i);
                    }

                    GUILayout.Space(50f);
                    EditorGUILayout.EndHorizontal();
                }

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                string text = null;
                GUIStyle gUIStyle2 = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button));
                gUIStyle2.fontSize = 14;
                gUIStyle2.fixedWidth = 75f;
                gUIStyle2.alignment = TextAnchor.MiddleLeft;
                if (GUILayout.Button(addButtonLabel, gUIStyle2))
                {
                    text = EditorUtility.OpenFolderPanel("添加目录", "", "");
                    ScanSettingWindow.I.Repaint();
                }

                GUILayout.Space(50f);
                GUILayout.EndHorizontal();
                if (!string.IsNullOrEmpty(text))
                {
                    text = ToolsHelper.ConvertAbsolutePathToRelative(text).Replace("%20", " ");
                    if (!directoryList.Contains(text))
                    {
                        directoryList.Add(text);
                    }
                }
            }
        }

        private static Texture2D texture2D_0;

        private static GUIStyle guistyle_0;

        private static GUIContent guicontent_0 = new GUIContent("选择并读取文件", "注意：读取数据的同时会确认资源在项目中的存在与否");

        public static Color GetShadowColor()
        {
            float num = EditorGUIUtility.isProSkin ? 0.12f : 0.6f;
            return new Color(num, num, num, 1.333f);
        }

        public static void LabelWithStyle(string labelText)
        {
            GUIStyle style = new GUIStyle
            {
                fontStyle = FontStyle.Bold,
                fontSize = 16,
                normal =
                {
                    textColor = GUI.skin.label.normal.textColor
                }
            };
            GUILayout.Label(labelText, style);
        }

        public static void DrawBox(bool bool_0)
        {
            GUIStyle gUIStyle = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.box));
            gUIStyle.border = new RectOffset(2, 2, 2, 2);
            Rect rect = EditorGUILayout.BeginVertical(gUIStyle);
            if (bool_0)
            {
                DrawBoxShadow(rect, 1f);
            }
        }

        public static void EndVertical()
        {
            EditorGUILayout.EndVertical();
        }

        public static void DrawToggleButton(string label, ref bool value, int labelWidth, string trueLabel, string falseLabel)
        {
            EditorGUILayout.BeginHorizontal("Box", GUILayout.ExpandWidth(expand: true));
            if (labelWidth == 0)
            {
                GUILayout.Label(label, GUILayout.ExpandWidth(expand: true));
            }
            else
            {
                GUILayout.Label(label, GUILayout.Width(labelWidth));
            }

            GUIStyle style = value ? Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonEnable) : Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonDisable);
            value = GUILayout.Toggle(value, value ? trueLabel : falseLabel, style, GUILayout.ExpandWidth(expand: false));
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }

        public static void DrawBox(float width, bool drawShadow)
        {
            GUIStyle gUIStyle = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.box));
            gUIStyle.border = new RectOffset(2, 2, 2, 2);
            Rect rect = EditorGUILayout.BeginVertical(gUIStyle, GUILayout.Width(width));
            if (drawShadow)
            {
                DrawBoxShadow(rect, 1f);
            }
        }

        public static void DrawBox(Rect rect_0, Color color_0)
        {
            if (texture2D_0 == null)
            {
                texture2D_0 = new Texture2D(1, 1);
            }

            if (guistyle_0 == null)
            {
                guistyle_0 = new GUIStyle();
            }

            texture2D_0.SetPixel(0, 0, color_0);
            texture2D_0.Apply();
            guistyle_0.normal.background = texture2D_0;
            GUI.Box(rect_0, GUIContent.none, guistyle_0);
        }

        public static void DrawBoxShadow(Rect rect, float shadowSize)
        {
            if (Event.current.type == EventType.Repaint)
            {
                Color color = GUI.color;
                GUI.color *= GetShadowColor();
                GUI.DrawTexture(new Rect(rect.x - shadowSize, rect.y - shadowSize, rect.width + 2f * shadowSize, shadowSize), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(rect.x - shadowSize, rect.yMax, rect.width + 2f * shadowSize, shadowSize), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(rect.x - shadowSize, rect.y - shadowSize, shadowSize, rect.height + 2f * shadowSize), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(rect.xMax, rect.y - shadowSize, shadowSize, rect.height + 2f * shadowSize), EditorGUIUtility.whiteTexture);
                GUI.color = color;
            }
        }

        public static void DrawLable_FontSize16_Bold(string message)
        {
            GUIStyle gUIStyle = new GUIStyle(GUI.skin.label);
            gUIStyle.fontStyle = FontStyle.Bold;
            gUIStyle.fontSize = 16;
            gUIStyle.normal.textColor = GUI.skin.label.normal.textColor;
            GUILayout.Label(message, gUIStyle);
        }

        public static Texture2D CreateTexture(int width, int height, Color color)
        {
            Color[] array = new Color[width * height];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = color;
            }

            Texture2D texture2D = new Texture2D(width, height);
            texture2D.SetPixels(array);
            texture2D.Apply();
            texture2D.hideFlags = HideFlags.HideAndDontSave;
            return texture2D;
        }

        //
        // 摘要:
        //     分割线
        //
        // 参数:
        //   height:
        public static void DrawSeparator(float height)
        {
            DrawSeparator(height, 3f, 12);
        }

        private static void DrawSeparator(float width, float height, int thickness)
        {
            Color color = GUI.color * GetShadowColor();
            Rect controlRect;
            if (width == -1f)
            {
                GUILayoutOption[] options = new GUILayoutOption[1]
                {
                    GUILayout.Height((float)thickness + height)
                };
                controlRect = EditorGUILayout.GetControlRect(options);
            }
            else
            {
                GUILayoutOption[] options2 = new GUILayoutOption[2]
                {
                    GUILayout.Height((float)thickness + height),
                    GUILayout.Width(width)
                };
                controlRect = EditorGUILayout.GetControlRect(options2);
            }

            controlRect.height = height;
            controlRect.y += thickness / 2;
            EditorGUI.DrawRect(controlRect, color);
            EditorGUILayout.Space();
        }

        public static void DrawColoredRectangle(float rectangleWidth)
        {
            SetColorAndDrawRectangle(new Color(0.1f, 0.1f, 0.1f, 1f), rectangleWidth, 1.5f, 10);
        }

        public static void SetColorAndDrawRectangle(Color color, float rectangleWidth, float rectangleHeight, int extraHeight)
        {
            Rect controlRect;
            if (rectangleWidth == -1f)
            {
                GUILayoutOption[] options = new GUILayoutOption[1]
                {
                    GUILayout.Height((float)extraHeight + rectangleHeight)
                };
                controlRect = EditorGUILayout.GetControlRect(options);
            }
            else
            {
                GUILayoutOption[] options2 = new GUILayoutOption[2]
                {
                    GUILayout.Height((float)extraHeight + rectangleHeight),
                    GUILayout.Width(rectangleWidth)
                };
                controlRect = EditorGUILayout.GetControlRect(options2);
            }

            controlRect.height = rectangleHeight;
            controlRect.y += extraHeight / 2;
            EditorGUI.DrawRect(controlRect, color);
            EditorGUILayout.Space();
        }

        public static void ApplyLayoutOption(float float_0, bool bool_0)
        {
            GUIStyle gUIStyle = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.box));
            gUIStyle.border = new RectOffset(2, 2, 2, 2);
            GUILayoutOption[] options = new GUILayoutOption[1]
            {
                GUILayout.Width(float_0)
            };
            Rect rect = EditorGUILayout.BeginVertical(gUIStyle, options);
            if (bool_0)
            {
                DrawBoxShadow(rect, 1f);
            }
        }

        public static void DrawStyledVerticalLayout(bool drawShadow)
        {
            GUIStyle gUIStyle = new GUIStyle(Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.box));
            gUIStyle.border = new RectOffset(2, 2, 2, 2);
            Rect rect = EditorGUILayout.BeginVertical(gUIStyle);
            if (drawShadow)
            {
                DrawBoxShadow(rect, 1f);
            }
        }

        public static void BeginStyledHorizontalLayout()
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space();
        }

        public static void EndStyledHorizontalLayout()
        {
            EditorGUILayout.Space();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
        }

        //
        // 摘要:
        //     创建自定义控件，包含两个标签和对应的切换按钮 - smethod_18
        //
        // 参数:
        //   label1:
        //     第一个标签的文本
        //
        //   label2:
        //     第二个标签的文本
        //
        //   width:
        //     标签的宽度
        //
        //   initialValue1:
        //     第一个切换按钮的初始值
        //
        //   initialValue2:
        //     第二个切换按钮的初始值
        //
        // 返回结果:
        //     包含两个切换按钮值的TCarrier对象
        public static ToolsHelper.Pair<bool, bool> CreateControl(string label1, string label2, int width, bool initialValue1, bool initialValue2)
        {
            DrawStyledVerticalLayout(drawShadow: false);
            GUILayout.BeginHorizontal();
            GUILayout.Label(label1, GUILayout.Width(width));
            initialValue1 = EditorGUILayout.Toggle(initialValue1);
            GUILayout.Label(label2, GUILayout.Width(width));
            initialValue2 = EditorGUILayout.Toggle(initialValue2);
            GUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();
            return new ToolsHelper.Pair<bool, bool>(initialValue1, initialValue2);
        }

        //
        // 摘要:
        //     显示一个带标签与轻触按钮的界面元素。当点击触发按钮后，其状态将更新，并返回最新的状态。 - smethod_19
        //
        // 参数:
        //   labelText:
        //     标签的文字内容
        //
        //   width:
        //     标签的宽度
        //
        //   toggleFlag:
        //     触发按钮的初始状态。为真时，表示选中状态。
        //
        //   endVerticalFlag:
        //     指示是否需要结束垂直布局。
        //
        // 返回结果:
        //     返回更新后的触发按钮状态。为真时，表示选中状态。
        public static bool DisplayGuiElement(string labelText, int width, bool toggleFlag, bool endVerticalFlag)
        {
            if (endVerticalFlag)
            {
                DrawStyledVerticalLayout(drawShadow: false);
            }

            GUILayout.BeginHorizontal();
            GUILayout.Label(labelText, GUILayout.Width(width));
            toggleFlag = EditorGUILayout.Toggle(toggleFlag);
            GUILayout.EndHorizontal();
            if (endVerticalFlag)
            {
                EditorGUILayout.EndVertical();
            }

            return toggleFlag;
        }

        public static float FloatField(string label, int width, float number, GUIStyle style)
        {
            GUILayout.BeginHorizontal();
            GUILayoutOption gUILayoutOption = GUILayout.Width(width);
            if (style != null)
            {
                GUILayout.Label(label, style, gUILayoutOption);
            }
            else
            {
                GUILayout.Label(label, gUILayoutOption);
            }

            string s = EditorGUILayout.TextField(number.ToString(), Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.textField));
            try
            {
                number = float.Parse(s);
            }
            catch (Exception ex)
            {
                ScanLog.Instance.LogInfo("DrawHorizontalLayoutWithFloatField", ex.Message + "\n" + ex.StackTrace);
                EditorUtility.DisplayDialog("Proj Scan提示", "输入格式错误，请正确填写浮点数（不包含f）", "确认");
            }

            GUILayout.EndHorizontal();
            return number;
        }

        public static int ExecutePopupMethod(string displayLabel, int selectedIndex, string[] displayOptions, float width, float labelWidth, bool withVerticalLayout)
        {
            if (withVerticalLayout)
            {
                DrawStyledVerticalLayout(drawShadow: false);
            }

            int fontSize = EditorStyles.popup.fontSize;
            float fixedHeight = EditorStyles.popup.fixedHeight;
            int fontSize2 = EditorStyles.label.fontSize;
            float fixedHeight2 = EditorStyles.label.fixedHeight;
            float labelWidth2 = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = labelWidth;
            EditorStyles.label.fontSize = 15;
            EditorStyles.label.fixedHeight = 23f;
            GUILayoutOption[] options = new GUILayoutOption[1]
            {
                GUILayout.Width(width)
            };
            selectedIndex = EditorGUILayout.Popup(displayLabel, selectedIndex, displayOptions, EditorStyles.popup, options);
            EditorStyles.popup.fontSize = fontSize;
            EditorStyles.popup.fixedHeight = fixedHeight;
            EditorStyles.label.fontSize = fontSize2;
            EditorStyles.label.fixedHeight = fixedHeight2;
            EditorGUIUtility.labelWidth = labelWidth2;
            if (withVerticalLayout)
            {
                EditorGUILayout.EndVertical();
            }

            return selectedIndex;
        }

        public static void DrawGUIElements(string labelText, string dialogTitle, ref string textValue, float width)
        {
            ApplyLayoutOption(width, bool_0: true);
            GUILayout.BeginHorizontal(GUILayout.Width(width - 18f));
            GUILayout.Label(labelText, GUILayout.Width(180f));
            textValue = EditorGUILayout.TextField(textValue, Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.textField));
            GUILayout.EndHorizontal();
            string directory = Application.dataPath;
            if (!string.IsNullOrEmpty(textValue))
            {
                directory = Path.GetDirectoryName(textValue);
            }

            GUILayout.BeginHorizontal();
            if (GUILayout.Button(guicontent_0, Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button), GUILayout.Width(180f)))
            {
                string str = EditorUtility.OpenFilePanel(dialogTitle, directory, "");
                Debug.Log("选择的路径：" + str);
            }

            GUILayout.FlexibleSpace();
            if (GUILayout.Button("读取此文件中所有的失败数据", Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button), GUILayout.Width(220f)))
            {
                Debug.Log("读取此文件中所有的失败数据：");
            }

            GUILayout.Space(50f);
            GUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();
        }
    }
}